package net.newsmth.smthapp.android.model;

import android.util.AttributeSet;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;
import java.util.List;

/**
 * Created by Hatsune Miku on 5/9/14.
 */
public class Article {

    private static String JSON_ID = "id";
    private static String JSON_BODY = "body";
    private static String JSON_TIME = "time";
    private static String JSON_FLAGS = "flags";
    private static String JSON_SUBJECT = "subject";
    private static String JSON_AUTHOR_ID = "author_id";
    private static String JSON_ATTACHMENT_COUNT = "attachments";
    private static String JSON_EFFSIZE = "effsize";
    private static String JSON_ATTACHMENT_LIST = "attachment_list";

    //<editor-fold desc="for attachment">
    private static String JSON_POSITION = "pos";
    private static String JSON_SIZE = "size";
    private static String JSON_NAME = "name";
    //</editor-fold>

    public static Article convertFromJson(JSONObject jsonObject) {
        Article result = null;

        try {
            result = new Article();
            result.setId(jsonObject.getLong(JSON_ID));
            result.setBody(jsonObject.getString(JSON_BODY));
            result.setCreatedTime(new Date(jsonObject.getLong(JSON_TIME)));
            result.setSubject(jsonObject.getString(JSON_SUBJECT));
            result.setAuthorId(jsonObject.getString(JSON_AUTHOR_ID));

            convertAttachments(result, jsonObject);
        } catch (Exception ex)
        {}

        return result;
    }

    private static void convertAttachments(Article article, JSONObject jsonObject) {
        int attachmentCount = 0;
        try {
            attachmentCount = jsonObject.getInt(JSON_ATTACHMENT_COUNT);
        } catch (JSONException ex) {
        }

        try {
            if (attachmentCount > 0) {
                JSONArray attachmentArray = jsonObject.getJSONArray(JSON_ATTACHMENT_LIST);

                for (int i = 0; i < attachmentCount; i++) {
                    JSONObject attachObject = attachmentArray.getJSONObject(i);
                    Attachment attachment = new Attachment();
                    attachment.setPosition(attachObject.getLong(JSON_POSITION));
                    attachment.setSize(attachObject.getLong(JSON_SIZE));
                    attachment.setName(attachObject.getString(JSON_NAME));

                    article.attachmentList.add(attachment);
                }
            }
        } catch (JSONException ex) {
        }
    }


    private long id;
    private String subject;
    private String body;
    private Date createdTime;
    private String flag;
    private List<Attachment> attachmentList;
    private String authorId;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public Date getCreatedTime() {
        return createdTime;
    }

    public void setCreatedTime(Date createdTime) {
        this.createdTime = createdTime;
    }

    public String getFlag() {
        return flag;
    }

    public void setFlag(String flag) {
        this.flag = flag;
    }

    public List<Attachment> getAttachmentList() {
        return attachmentList;
    }

    public void setAttachmentList(List<Attachment> attachmentList) {
        this.attachmentList = attachmentList;
    }

    public String getAuthorId() {
        return authorId;
    }

    public void setAuthorId(String authorId) {
        this.authorId = authorId;
    }

}
